//
//  MCRechargeTableViewCell.m
//  TLYL
//
//  Created by MC on 2017/9/22.
//  Copyright © 2017年 TLYL01. All rights reserved.
//

#import "MCRechargeTableViewCell.h"
#import "MCPaySelectedLotteryPickView.h"
#define UILABEL_Kern_SPACE 0.1
#define UILABEL_LINE_SPACE 5
#define TIP @"温馨提示：\n1.点击确认充值按钮后会通过手机浏览器进入充值页面；\n2.充值完成后，重新进入APP查看余额；\n3.请在30分钟内完成充值"
#define MoneyArray @[@"5000",@"3000",@"2000",@"1000",@"500",@"300",@"200",@"100",@"50"]
#define MoneyArr2  @[@"100",@"200",@"500",@"1000"]

@interface MCRechargeTableViewCell ()
<
UICollectionViewDelegate,
UICollectionViewDataSource
>

@property(nonatomic, strong)UICollectionView * selectedCollectionView;
@property (nonatomic,strong)NSString * minRecMoney    ;//此充值方式最低充值额
@property (nonatomic,strong)NSString * maxRecMoney    ;//此充值方式最高充值额
@property (nonatomic,strong)UIButton * btn1;
@property (nonatomic,strong)UIView * downView ;
@property (nonatomic,strong)UIView * DownView1;
@property (nonatomic,strong)UIView * DownView2;
@property (nonatomic,strong)UILabel * textLab;
@end

@implementation MCRechargeTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self){
        [self initView];
    }
    return self;
}


- (void)initView{
    self.backgroundColor=[UIColor clearColor];
    
    UIView * upView=[[UIView alloc]init];
    upView.frame=CGRectMake(0, 0, G_SCREENWIDTH, 131);
    upView.backgroundColor=[UIColor clearColor];
    [self addSubview:upView];
    
    CGFloat W = 30;
    
    /*
     * 左滑按钮
     */
    UIButton * leftbtn=[[UIButton alloc]init];
    [upView addSubview:leftbtn];
    [self setBtn:leftbtn andImgName:@"MCRecharge_zuohua" andTag:1001 andText:nil];
    [leftbtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(upView.mas_centerY);
        make.left.equalTo(self.mas_left).offset(10);
        make.height.mas_equalTo(W);
        make.width.mas_equalTo(W);
    }];
    
    /*
     * 右滑按钮
     */
    UIButton * rightbtn=[[UIButton alloc]init];
    [upView addSubview:rightbtn];
    [self setBtn:rightbtn andImgName:@"MCRecharge_youhua" andTag:1002 andText:nil];
    [rightbtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(upView.mas_centerY);
        make.right.equalTo(self.mas_right).offset(-10);
        make.height.mas_equalTo(W);
        make.width.mas_equalTo(W);
    }];
    
    [upView addSubview:self.selectedCollectionView];
    self.selectedCollectionView.backgroundColor=[UIColor clearColor];
    [self.selectedCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(upView.mas_centerY);
        make.left.equalTo(upView.mas_left).offset(50);
        make.right.equalTo(upView.mas_right).offset(-50);
        make.height.mas_equalTo(80);
    }];
    
    CGFloat down_H =150;
    
    UIView * downView = [[UIView alloc]init];
    [self addSubview:downView];
    downView.layer.cornerRadius=5;
    downView.clipsToBounds=YES;
    downView.backgroundColor=[UIColor whiteColor];
    downView.frame=CGRectMake(10, 131, G_SCREENWIDTH-20, down_H);
    
    /*
     * 充值方式
     */
    UILabel * lab1=[[UILabel alloc]init];
    [downView addSubview:lab1];
    lab1.frame=CGRectMake(10, 0, 60, down_H/3.0);
    [self setLab:lab1 andColor:RGB(46, 46, 46) andFont:14 andText:@"充值方式"];
    
    UIButton * btn1 = [[UIButton alloc]init];
    [downView addSubview:btn1];
    btn1.backgroundColor=[UIColor clearColor];
    btn1.frame=CGRectMake(100, 0, G_SCREENWIDTH-120, down_H/3.0);
    [btn1 setTitle:@"请选择" forState:UIControlStateNormal];
    btn1.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    btn1.titleLabel.font    = [UIFont systemFontOfSize: 14];
    [btn1 setTitleColor:RGB(46, 46, 46) forState:UIControlStateNormal];
    [btn1 addTarget:self action:@selector(selectedRechargeType) forControlEvents:UIControlEventTouchUpInside];
    _btn1=btn1;
    
    UIImageView * arrow=[[UIImageView alloc]init];
    [btn1 addSubview:arrow];
    arrow.image=[UIImage imageNamed:@"person-icon-more"];
    arrow.userInteractionEnabled=NO;
    arrow.frame=CGRectMake(G_SCREENWIDTH-120-10-9, (50-16)/2.0, 9, 16);
    
    
    /*
     * 充值金额
     */
    UITextField * textField=[[UITextField alloc]init];
    [downView addSubview:textField];
    textField.frame=CGRectMake(100, down_H/3.0, G_SCREENWIDTH-120, down_H/3.0);
    _textField=textField;
    textField.textAlignment = NSTextAlignmentLeft;
    textField.returnKeyType = UIReturnKeyDone;
    textField.keyboardType = UIKeyboardTypeDecimalPad;
    [_textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:(UIControlEventEditingChanged)];
    textField.font=[UIFont systemFontOfSize:14];
    [textField setValue:[UIFont systemFontOfSize:13] forKeyPath:@"_placeholderLabel.font"];
    
    if (_minRecMoney&&_maxRecMoney) {
        _textField.placeholder=[NSString stringWithFormat:@"充值金额范围：%@~%@元",_minRecMoney,_maxRecMoney];
    }
    
    
    //ID:   230. 231.  232
    UIButton * btn_SearchMoney = [[UIButton alloc]init];
    [downView addSubview:btn_SearchMoney];
    btn_SearchMoney.backgroundColor=[UIColor clearColor];
    [btn_SearchMoney setTitleColor:RGB(46, 46, 46) forState:UIControlStateNormal];
    btn_SearchMoney.frame=CGRectMake(100, down_H/3.0, G_SCREENWIDTH-120, down_H/3.0);
    [btn_SearchMoney setTitle:@"充值金额" forState:UIControlStateNormal];
    _btn_SearchMoney = btn_SearchMoney;
    [_btn_SearchMoney addTarget:self action:@selector(OpenSearchMoneyList:) forControlEvents:UIControlEventTouchUpInside];
    _btn_SearchMoney.hidden=YES;
    _btn_SearchMoney.titleLabel.font = [UIFont systemFontOfSize:14];
    _btn_SearchMoney.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    
    
    UILabel * lab2=[[UILabel alloc]init];
    [downView addSubview:lab2];
    lab2.frame=CGRectMake(10, down_H/3.0, 60, down_H/3.0);
    [self setLab:lab2 andColor:RGB(46, 46, 46) andFont:14 andText:@"充值金额"];
    
    UIView * line1=[[UIView alloc]init];
    line1.frame=CGRectMake(10, down_H/3.0, G_SCREENWIDTH-20-10, 0.5);
    [downView addSubview:line1];
    line1.backgroundColor=RGB(213,213,213);
    
    UIView * line2=[[UIView alloc]init];
    line2.frame=CGRectMake(10, down_H*2/3.0, G_SCREENWIDTH-20-10, 0.5);
    [downView addSubview:line2];
    line2.backgroundColor=RGB(213,213,213);
    
    /*
     * 快速选择
     */
    UILabel * lab3=[[UILabel alloc]init];
    [downView addSubview:lab3];
    lab3.frame=CGRectMake(10, down_H*2/3.0, 60, down_H/3.0);
    [self setLab:lab3 andColor:RGB(46, 46, 46) andFont:14 andText:@"快速选择"];
    
    
//    int i=0;
//    NSArray * arr=@[@"100",@"200",@"500",@"1000"];
//    for (NSString * str in arr) {
//        UIButton * sbtn=[[UIButton alloc]init];
//        sbtn.backgroundColor=RGB(255, 170, 47);
//        sbtn.layer.cornerRadius=3;
//        sbtn.clipsToBounds=YES;
//        [sbtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//        sbtn.titleLabel.font=[UIFont systemFontOfSize:12];
//        [self setBtn:sbtn andImgName:nil andTag:[str integerValue] andText:str];
//        [downView addSubview:sbtn];
//        CGFloat W_sbtn=(G_SCREENWIDTH-20-100-15-10)/4.0;
//        sbtn.frame=CGRectMake(100+i*(W_sbtn+5),down_H*2/3.0+(down_H/3.0-21)/2.0 , W_sbtn, 21);
//        i++;
//    }
    
    
    UIView * DownView1 = [[UIView alloc]init];
    [downView addSubview:DownView1];
    DownView1.backgroundColor=[UIColor clearColor];
    DownView1.frame=CGRectMake(0, down_H*2/3.0, G_SCREENWIDTH-20, down_H/3.0);
    _DownView1=DownView1;
    
    
    int i=0;
    NSArray * arr1=@[@"100",@"200",@"500",@"1000"];
    for (NSString * str in arr1) {
        UIButton * sbtn=[[UIButton alloc]init];
        sbtn.backgroundColor=RGB(255, 170, 47);
        sbtn.layer.cornerRadius=3;
        sbtn.clipsToBounds=YES;
        [sbtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        sbtn.titleLabel.font=[UIFont systemFontOfSize:12];
        [self setBtn:sbtn andImgName:nil andTag:[str integerValue] andText:str];
        [DownView1 addSubview:sbtn];
        CGFloat W_sbtn=(G_SCREENWIDTH-20-100-15-10)/4.0;
        sbtn.frame=CGRectMake(100+i*(W_sbtn+5),(down_H/3.0-21)/2.0 , W_sbtn, 21);
        i++;
    }
    
    
    
    UIView * DownView2 = [[UIView alloc]init];
    [downView addSubview:DownView2];
    DownView2.backgroundColor=[UIColor clearColor];
    DownView2.frame=CGRectMake(0, down_H*2/3.0, G_SCREENWIDTH-20, down_H/3.0);
    _DownView2=DownView2;
    _DownView2.hidden=YES;
    int j=0;
    NSArray * arr2=MoneyArr2;
    for (NSString * str in arr2) {
        UIButton * sbtn=[[UIButton alloc]init];
        sbtn.backgroundColor=RGB(255, 170, 47);
        sbtn.layer.cornerRadius=3;
        sbtn.clipsToBounds=YES;
        [sbtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        sbtn.titleLabel.font=[UIFont systemFontOfSize:12];
        [self setBtn:sbtn andImgName:nil andTag:[str integerValue] andText:str];
        [DownView2 addSubview:sbtn];
        CGFloat W_sbtn=(G_SCREENWIDTH-20-100-15-10)/4.0;
        sbtn.frame=CGRectMake(100+j*(W_sbtn+5),(down_H/3.0-21)/2.0 , W_sbtn, 21);
        j++;
    }
    
    
//    UILabel * tip=[[UILabel alloc]init];
//    tip.numberOfLines=0;
//    [self addSubview:tip];
//    [self setLab:tip andColor:RGB(136,136,136) andFont:10 andText:TIP];
//    [self setLabelSpace:tip withValue:TIP withFont:[UIFont systemFontOfSize:10]];
//    [tip mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(self.mas_left).offset(19);
//        make.right.equalTo(self.mas_right).offset(-19);
//        make.top.equalTo(downView.mas_bottom).offset(44);
//    }];
    
    
    
    UILabel * textLab = [[UILabel alloc]init];
    textLab.font = [UIFont systemFontOfSize:11];
    textLab.textColor = RGB(136,136,136);
    textLab.text=@"实际到账金额：0\n(实际充值到账金额=充值金额-手续费)";
    [self addSubview:textLab];
    textLab.numberOfLines = 0;
    _textLab=textLab;
    textLab.textAlignment = NSTextAlignmentCenter;
    [textLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(19);
        make.right.equalTo(self.mas_right).offset(-19);
        make.top.equalTo(downView.mas_bottom).offset(1);
    }];
    [self caculateSXF];
}


#pragma mark-弹出金额选择框
-(void)OpenSearchMoneyList:(UIButton *)btn{
    
    __weak __typeof__ (self) wself = self;
    
    MCPaySelectedLotteryPickView *picker = [[MCPaySelectedLotteryPickView alloc]init];
    picker.Type=MCPaySelectedPickViewType_Str;
    picker.title=@"充值金额";
    picker.dataSource = MoneyArray;
    picker.getCountBlock = ^(NSString *t) {
        wself.textField.text=t;
        [wself.btn_SearchMoney setTitle:t forState:UIControlStateNormal];
        [wself.btn_SearchMoney setTitleColor:RGB(46, 46, 46) forState:UIControlStateNormal];
        
    };
    [picker show];
    
}


#pragma mark-根据选择不同的充值方式  变化充值金钱的范围
-(void)setRangeOfMoney:(MCPaymentNewModel *)model{
    
    _minRecMoney=model.MinRecMoney;
    _maxRecMoney=model.MaxRecMoney;
    _textField.placeholder=[NSString stringWithFormat:@"充值金额范围：%@~%@元",_minRecMoney,_maxRecMoney];
    [_btn_SearchMoney setTitle:[NSString stringWithFormat:@"充值金额范围：%@~%@元",_minRecMoney,_maxRecMoney] forState:UIControlStateNormal];
    [_btn_SearchMoney setTitleColor:RGB(223, 223, 223) forState:UIControlStateNormal];
    
    [self caculateSXF];
}

-(void)setLab:(UILabel *)lab andColor:(UIColor*)color andFont:(CGFloat)font andText:(NSString *)text{
    lab.font = [UIFont systemFontOfSize:font];
    lab.textColor=color;
    lab.text=text;
    lab.textAlignment = NSTextAlignmentLeft;
    
}
-(void)setBtn:(UIButton *)btn andImgName:(NSString *)imgName andTag:(NSInteger)tag andText:(NSString *)text{
    btn.tag=tag;
    if (imgName) {
        [btn setImage:[UIImage imageNamed:imgName] forState:UIControlStateNormal];
    }
    if (text) {
        [btn setTitle:text forState:UIControlStateNormal];
    }
    [btn addTarget:self action:@selector(action_Btn:) forControlEvents:UIControlEventTouchUpInside];
}

-(void)action_Btn:(UIButton *)btn{
    //左边滑动
    if (btn.tag==1001) {
        
        
        NSArray * arr =[self.selectedCollectionView indexPathsForVisibleItems ];
        NSMutableArray * marr=[NSMutableArray arrayWithArray:arr];
        if (arr.count>4) {
            NSIndexPath * index=arr[0];
            for (NSIndexPath * indexP in arr) {
                if (index.row>indexP.row) {
                    index=indexP;
                }
            }
            [marr removeObject:index];
        }
        
        NSIndexPath * m_index=marr[0];
        for (NSIndexPath * indexP in marr) {
            if (m_index.row>indexP.row) {
                m_index=indexP;
            }
        }
        
        NSUInteger row =m_index.row;
        if (row>0) {
            row--;
        }
        NSIndexPath *indexPP=[NSIndexPath indexPathForRow:row inSection:m_index.section];
        
        [self.selectedCollectionView scrollToItemAtIndexPath:indexPP atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
        
        //右边滑动
    }else if (btn.tag==1002){
        if (self.dataSource.count<2) {
            return;
        }
        NSArray * arr =[self.selectedCollectionView indexPathsForVisibleItems ];
        NSMutableArray * marr=[NSMutableArray arrayWithArray:arr];
        if (arr.count>4) {
            NSIndexPath * index=arr[0];
            for (NSIndexPath * indexP in arr) {
                if (index.row<indexP.row) {
                    index=indexP;
                }
            }
            [marr removeObject:index];
        }
        NSIndexPath * m_index=marr[0];
        for (NSIndexPath * indexP in marr) {
            if (m_index.row<indexP.row) {
                m_index=indexP;
            }
        }
        
        
        NSUInteger row =m_index.row;
        if (row<(self.dataSource.count-2)) {
            row++;
        }else{
            row=self.dataSource.count-1;
        }
        NSIndexPath *indexPP=[NSIndexPath indexPathForRow:row inSection:m_index.section];
        
        [self.selectedCollectionView scrollToItemAtIndexPath:indexPP atScrollPosition:UICollectionViewScrollPositionRight animated:YES];
        
    }else{
        _textField.text=[NSString stringWithFormat:@"%ld",(long)btn.tag];
        [_btn_SearchMoney setTitle:[NSString stringWithFormat:@"%ld",(long)btn.tag] forState:UIControlStateNormal];
        [_btn_SearchMoney setTitleColor:RGB(46, 46, 46) forState:UIControlStateNormal];
        
        [self caculateSXF];
        
    }
    
}

-(void)setDataSource:(NSMutableArray *)dataSource{
    _dataSource=dataSource;
    
    [self.selectedCollectionView reloadData];
    
    if (self.dataSource.count>0) {
        MCPayTypeModel *big_model=self.dataSource[0];
        MCPaymentNewModel *small_model = big_model.payType[0];
        [self setRangeOfMoney:small_model];
        [_btn1 setTitle:small_model.PayName forState:UIControlStateNormal];
        if ([self.delegate respondsToSelector:@selector(celldidSelectPaymentType:)]) {
            [self.delegate celldidSelectPaymentType:small_model];
        }
        
    }
    
}


-(void)textFieldDidChange:(UITextField *)textfield{
    
    if (textfield.text.length>12) {
        [textfield.text substringToIndex:12];
    }
    
    if (textfield.text.length == 1 && [textfield.text isEqualToString:@"."]) {
        textfield.text = @"0.";
    }
    NSArray *arr = [textfield.text componentsSeparatedByString:@"."];
    
    float f_num = [textfield.text floatValue];
    if (_maxRecMoney.length>0) {
        if (f_num>= [_maxRecMoney intValue]) {
            textfield.text =_maxRecMoney;
        }
    }
    
    if (arr.count >1) {
        
        NSString *str = arr[1];
        if (str.length > 2) {
            str = [str substringWithRange:NSMakeRange(0, 2)];
        }
        textfield.text = [NSString stringWithFormat:@"%@.%@",arr[0],str];
        
        NSString * str1=arr[0];
        if (_maxRecMoney.length>0) {
            if ([str1 isEqualToString:_maxRecMoney]) {
                textfield.text = _maxRecMoney;
            }
        }
    }
    
    [self caculateSXF];

}

//计算手续费
-(void)caculateSXF{
    
    //"FeeMoney":"0.1000",   --手续费范围
    NSString *FeeMoney=[NSString stringWithFormat:@"%@",_selectedModel.FeeMoney];;
    //"FeeType":"1",    --充值手续费类型   0 无手续费  1 比例  2 固定金额
    NSString * FeeType = [NSString stringWithFormat:@"%@",_selectedModel.FeeType];
    //"CappingMoney":"100.0000"   --封顶金额
    NSString *CappingMoney = [NSString stringWithFormat:@"%@",_selectedModel.CappingMoney];
    
    
    NSString * s_SXF = @"";
    NSString * s_FDJE = @"";
    
    //手续费
    double SXF =0;
    double C = [_textField.text floatValue];
    
    //比例
    if ([FeeType isEqualToString:@"1"]) {
        
        SXF = C*[FeeMoney floatValue]/100.0;
        if ([CappingMoney floatValue]<0.001) {
            
        }else{
            if (SXF > [CappingMoney floatValue]) {
                SXF = [CappingMoney floatValue];
            }
        }
        if ([FeeMoney floatValue]>0) {
            
            s_SXF =[NSString stringWithFormat:@",单笔手续费收取%@%%",GetRealSNum(FeeMoney)];

        }

        if ([CappingMoney floatValue]>0.001) {
            s_FDJE = [NSString stringWithFormat:@",%@元封顶",GetRealSNum(CappingMoney)];
        }
        
        //固定金额
    }else if ([FeeType isEqualToString:@"2"]){
        
        SXF = [FeeMoney floatValue];
        s_SXF =[NSString stringWithFormat:@",单笔手续费收取%@元",GetRealSNum(FeeMoney)];
        
        if ([CappingMoney floatValue]>0.001) {
            s_FDJE = [NSString stringWithFormat:@",%@元封顶",GetRealSNum(CappingMoney)];
        }
        
        //无手续费
    }else{
        SXF = 0;
    }
    //实际到账
    double  SJDZ = C-SXF;
    if (SJDZ<0) {
        SJDZ=0;
    }

//    if ([GetRealFloatNum(SJDZ) isEqualToString:@"0"]||([GetRealFloatNum(SJDZ) doubleValue]-[_textField.text doubleValue]<0.01)) {
    if([FeeType isEqualToString:@"0"]){
        _textLab.text=@"";
    }else{
        NSString * s_SJDZ = [NSString stringWithFormat:@"%.4f",SJDZ];
        if ([s_SJDZ isEqualToString:@"0.0000"]) {
            s_SJDZ=@"0";
        }
        
        NSString * str = [NSString stringWithFormat:@"实际到账金额：%@%@%@\n(实际充值到账金额=充值金额-手续费)",s_SJDZ,s_SXF,s_FDJE];
        
        NSRange range = [str rangeOfString:s_SJDZ];
        NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:str];
        
        //    // 修改富文本中的不同文字的样式
        //    [attri addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20] range:NSMakeRange(range.location, range.length-1)];
        
        // 设置数字为红色
        [attri addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(range.location, range.length)];
        
        _textLab.attributedText=attri;
    }
    
    
}


-(void)setUIIsWechatAndAliPay:(BOOL)isWechatAndAliPay{
    _textField.text=@"";
    if (isWechatAndAliPay) {
        
        _DownView1.hidden=YES;
        _DownView2.hidden=NO;
        _btn_SearchMoney.hidden=NO;
        _textField.hidden = YES;
        
    }else{
        
        _DownView1.hidden=NO;
        _DownView2.hidden=YES;
        _btn_SearchMoney.hidden=YES;
        _textField.hidden = NO;
        
    }
    
}
+(CGFloat)computeHeight:(NSUInteger)info{
    return 345+50-100;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

-(UICollectionView *)selectedCollectionView{
    if (!_selectedCollectionView) {
        
        //创建一个layout布局类
        MCCollectionViewFlowLayout * layout = [[MCCollectionViewFlowLayout alloc]init];
        //设置布局方向为横向流布局
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _selectedCollectionView=[[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _selectedCollectionView.backgroundColor=[UIColor clearColor];
        _selectedCollectionView.dataSource=self;
        _selectedCollectionView.delegate=self;
        [_selectedCollectionView registerClass:[MCRechargeCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([MCRechargeCollectionViewCell class])];
        
    }
    return _selectedCollectionView;
}
#pragma mark - <UICollectionViewDataSource>
// 设置headerView和footerView的
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    if (kind == UICollectionElementKindSectionHeader) {
        return nil ;
    }
    return nil ;
}

//设置section的颜色
- (UIColor *)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout colorForSectionAtIndex:(NSInteger)section{
    return [UIColor clearColor];
}
//设置item的size
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat W = ( G_SCREENWIDTH-50*2)/4.0;
    CGFloat H = 60 ;
    return CGSizeMake(W, H);
}
//设置section的边距
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(10, 0, 10, 0);
}
// 两个cell之间的最小间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}
// 两行之间的最小间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}
//numberOfItemsInSection
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (self.dataSource.count>0) {
        return self.dataSource.count;
    }
    return 0;
}

//numberOfSections
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}


//UICollectionViewCell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    UICollectionViewCell*  cell = nil;
    if (!cell) {
        cell =[collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([MCRechargeCollectionViewCell class]) forIndexPath:indexPath];
    }
    
    
    MCRechargeCollectionViewCell *ex_cell=(MCRechargeCollectionViewCell *)cell;
    if (self.dataSource.count>indexPath.row) {
        ex_cell.dataSource=self.dataSource[indexPath.row];
    }
    
    return cell;
}
#pragma mark-didSelect
#pragma mark-选择充值方式【大类】
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    MCPayTypeModel * big_model=self.dataSource[indexPath.row];
    big_model.isSelected=YES;
    
    for (MCPayTypeModel * bmodel in self.dataSource) {
        if ([big_model.ClientPayTypeName isEqualToString:bmodel.ClientPayTypeName]) {
            bmodel.isSelected=YES;
        }else{
            bmodel.isSelected=NO;
        }
    }
    [self.selectedCollectionView reloadData];
    
    MCPaymentNewModel * small_model =  big_model.payType[0];
    [_btn1 setTitle:small_model.PayName forState:UIControlStateNormal];
    
    [self setRangeOfMoney:small_model];
    
    /*
     * 选择付款方式
     */
    if ([self.delegate respondsToSelector:@selector(celldidSelectPaymentType:)]) {
        [self.delegate celldidSelectPaymentType:small_model];
    }
}

//给UILabel设置行间距和字间距
-(void)setLabelSpace:(UILabel*)label withValue:(NSString*)str withFont:(UIFont*)font {
    NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
    paraStyle.lineBreakMode = NSLineBreakByCharWrapping;
    paraStyle.alignment = NSTextAlignmentLeft;
    paraStyle.lineSpacing = UILABEL_LINE_SPACE; //设置行间距
    paraStyle.hyphenationFactor = 1.0;
    paraStyle.firstLineHeadIndent = 0.0;
    paraStyle.paragraphSpacingBefore = 0.0;
    paraStyle.headIndent = 0;
    paraStyle.tailIndent = 0;
    //设置字间距 NSKernAttributeName:@1.5f
    NSDictionary *dic = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:paraStyle, NSKernAttributeName:@UILABEL_Kern_SPACE
                          };
    
    NSAttributedString *attributeStr = [[NSAttributedString alloc] initWithString:str attributes:dic];
    label.attributedText = attributeStr;
}


#pragma mark-选择充值方式【小类】
-(void)selectedRechargeType{
    
    __weak __typeof__ (self) wself = self;
    
    
    MCPaySelectedLotteryPickView *picker = [[MCPaySelectedLotteryPickView alloc]init];
    picker.Type=MCPaySelectedPickViewType_RechargeType;
    
    for (MCPayTypeModel * big_model in self.dataSource) {
        if (big_model.isSelected) {
            picker.dataSource = big_model.payType;
        }
    }
    
    picker.title=@"请选择充值方式";
    picker.PaymentModelBlock = ^(MCPaymentNewModel *model) {
        
        [wself.btn1 setTitle:model.PayName forState:UIControlStateNormal];
        wself.selectedModel = model;
        [wself caculateSXF];
        
        if ([wself.delegate respondsToSelector:@selector(celldidSelectPaymentType:)]) {
            [wself.delegate celldidSelectPaymentType:model];
            [wself setRangeOfMoney:model];
        }
    };
    [picker show];
    
}

@end



















